#include "MainMenuView.h"

using namespace Views;

MainMenuView::MainMenuView(const glm::ivec2 &viewport, const File::ShaderLoaderPtr &shader_loader, const File::TextureLoaderPtr &tex_loader)
	: IView("MAIN_MENU", viewport)
{
	shader = shader_loader->load("present.vs", "", "layers.fs");

	tex_font = tex_loader->loadRelativePath("terminal.png", true);
	uni_font = std::make_shared<Render::Uniform>(shader->getFS(), "Font");

	float width = (float)tex_font->getWidth();
	float height = (float)tex_font->getHeight();

	fontsize = glm::vec2(width / 16.0f, height / 16.0f);
	uni_fontsize = std::make_shared<Render::Uniform>(shader->getFS(), "fontsize");
	uni_viewport = std::make_shared<Render::Uniform>(shader->getFS(), "viewport");

	int powerOfTwoWidth = 1;
    int powerOfTwoHeight = 1;
    while (powerOfTwoWidth < width)
            powerOfTwoWidth *= 2;
    while (powerOfTwoHeight < height)
            powerOfTwoHeight *= 2;

	viewport_coef = glm::vec2(width / (float)powerOfTwoWidth, height / (float)powerOfTwoHeight);
	uni_viewport_coef = std::make_shared<Render::Uniform>(shader->getFS(), "viewport_coef");

	layers.resize(1, nullptr);
	layers[MAIN_MENU_BACKGROUND_LAYER] = std::make_shared<ViewBuffer>(viewport); //std::unique_ptr<ViewBuffer>(new ViewBuffer(viewport));
	//setDefaultBackgroundColor(glm::ivec4(255, 255, 255, 255), MAIN_MENU_BACKGROUND_LAYER);
	//setDefaultForegroundColor(glm::ivec4(255, 0, 0, 255), MAIN_MENU_BACKGROUND_LAYER);
}

MainMenuView::~MainMenuView()
{
}

void MainMenuView::update(float dt)
{
	//UPDATE GAME LOGIC
	//...

	//UPDATE VIEW LAYERS WITH FRESH DATA
	for(unsigned int i = 0; i < layers.size(); i++)
	{
		auto layer = layers[i];
		if(layer->hasDirtyCharacters()) 
		{
			layer->pbo_characters->bufferFromDataOnCPU(layer->tex_characters->getData(), layer->characters_dirty, GL_STREAM_DRAW, GL_WRITE_ONLY);
			layer->pbo_characters->copyToTextureOnGPU(layer->tex_characters, 0);
		}
		if(layer->hasDirtyBackground())
		{
			layer->pbo_background->bufferFromDataOnCPU(layer->tex_background->getData(), layer->background_dirty, GL_STREAM_DRAW, GL_WRITE_ONLY);
			layer->pbo_background->copyToTextureOnGPU(layer->tex_background, 0);
		}
		if(layer->hasDirtyForeground()) 
		{
			layer->pbo_foreground->bufferFromDataOnCPU(layer->tex_foreground->getData(), layer->foreground_dirty, GL_STREAM_DRAW, GL_WRITE_ONLY);
			layer->pbo_foreground->copyToTextureOnGPU(layer->tex_foreground, 0);
		}
		layer->clearDirty();
	}
}

void MainMenuView::render()
{
	shader->bind();
	unsigned int layer_offset = 0;
	for(unsigned int i = 0; i < layers.size(); i++)
		layer_offset = layers[i]->bind(shader->getFS(), layer_offset);

	tex_font->bind();
	uni_font->bind((int)layer_offset);

	uni_fontsize->bind(fontsize);
	uni_viewport->bind(viewport);
	uni_viewport_coef->bind(viewport_coef);

	quad->render();
}

bool MainMenuView::keyInput(int key, int pressed)
{
	return false;
}

bool MainMenuView::mouseButtonInput(int button, int pressed)
{
	return false;
}

bool MainMenuView::mouseMoveInput(int x, int y)
{
	return false;
}

